package com.kgc.publicconsumer.controller;

import com.kgc.publiccommon.util.ResponseMsg;
import com.kgc.publicconsumer.feign.UserFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserFeign userFeign;


    @RequestMapping("login")
    public ResponseMsg login(String name, String password, String checkCode) {
        logger.info("consumer UserController login name:{},password:{},checkCode:{}", name, password, checkCode);
        ResponseMsg msg = userFeign.login(name, password, checkCode);
        logger.debug("consumer UserController login msg:{}", msg);
        return msg;
    }

    @RequestMapping("getCaptcha")
    public void getCaptcha(HttpServletResponse response) throws IOException {
        String base64Image = userFeign.getCaptcha();
        if (base64Image != null) {
            response.setContentType("image/jpeg");
            byte[] imageBytes = Base64.getDecoder().decode(base64Image);
            response.getOutputStream().write(imageBytes);
        }
    }

    @RequestMapping("logout")
    public ResponseMsg logout(@RequestBody Map<String, String> request, HttpSession session) {
        logger.info("consumer UserController logout is start...");
        String sessionId = request.get("sessionId");
        if (sessionId == null || sessionId.isEmpty()) {
            ResponseMsg msg = new ResponseMsg();
            msg.setCode("400");
            msg.setMsg("缺少sessionId参数");
            return msg;
        }
        Map<String, String> feignRequest = new HashMap<>();
        feignRequest.put("sessionId", sessionId);
        ResponseMsg msg = userFeign.logout(feignRequest);
        session.invalidate();
        logger.debug("consumer UserController msg:{}", msg);
        return msg;
    }


    @RequestMapping("selectRoleById")
    public ResponseMsg selectRoleById(String id) {
        logger.info("consumer UserController selectRoleById id:{}", id);
        return userFeign.selectRoleById(Integer.parseInt(id));
    }

    @RequestMapping("selectAllUser")
    ResponseMsg selectAllUser(){
        logger.info("consumer UserController selectAllUser");
        return userFeign.selectAllUser();
    }
    @RequestMapping("selectUserByDept")
    public ResponseMsg selectUserByDept(@RequestBody Map<String, Object> map){
        logger.info("consumer UserController selectUserByDept");
        return userFeign.selectUserByMap(map);
    }
}
